package Flink.Partitioning_Phy;

import org.apache.flink.api.common.functions.Partitioner;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class PartitionCustomDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(
                new Configuration());

        env.setParallelism(2);

        DataStreamSource<String> socketDS = env.socketTextStream("master", 7777);
        DataStream<String> myDS = socketDS.partitionCustom(
                new MyPartitioner()
                , value -> value);
        myDS.print();

        env.execute();
    }

    public static class MyPartitioner implements Partitioner<String> {

        @Override
        public int partition(String key, int numPartitions) {
            return Integer.parseInt(key) % numPartitions;
        }
    }
}
